package com.woniuxy.crm.unilt;

/**
 * @author 老谭 (<a href="http://www.woniuxy.com">蜗牛学苑</a>)
 */
public class ObjectList<T> {
    private T[] elementData;  // 最终存数据的位置
    private int size;   // 存储数据的实际数量

    public ObjectList() {
        elementData = (T[]) new Object[4];

    }

    public ObjectList(int capacity) {
        if (capacity < 0) {
            System.out.println("容量不能为负");
            return;
        }
        elementData = (T[]) new Object[capacity];
    }

    public void add(T data) {
        if (size == elementData.length) {
            T[] newelementData = (T[]) new Object[elementData.length * 2];
            System.arraycopy(elementData, 0, newelementData, 0, size);
            elementData = newelementData;
        }
        elementData[size++] = data;

    }

    public int size() {
        return this.size;
    }

    public T get(int index) {
        if (index < 0 || index >= size) {
            System.out.println("索引无效");
            return null;
        }
        return elementData[index];
    }


    public boolean contains(T o) {
        if (o == null) {
            for (int i = 0; i < size; i++) {
                if (o == elementData[i]) {
                    return true;
                }
            }

        } else {
            for (int i = 0; i < size; i++) {
                if (o.equals(elementData[i])) {
                    return true;
                }
            }
            return false;
        }
        return false;
    }

    public boolean isEmpty() {

        return size == 0;
    }

    public void remove(Integer index) {
        if (index >= 0 && index < size) {
            if (index != (size - 1)) {
                System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
                size--;
            }

        }
    }

    public void remove(T o) {
        if (o != null) {
            for (int i = 0; i < size; i++) {
                if (o.equals(elementData[i])) {
                    remove(i);
                    break;
                } else {
                    System.out.println("数据不存在");
                    return;
                }
            }
        } else {
            System.out.println("数据不能为空");
            return;
        }
    }
    @Override
    public  String toString(){
        if(size==0){
            return  "[]";
        }
        else {
            StringBuilder sb = new StringBuilder("[");
            for (int i = 0; i < size; i++) {
                sb.append(elementData[i]);
                if (i<size-1){
                    sb.append(",");
                }
            }
            sb.append("]");
            return  sb.toString();
        }
    }

    public void getAll() {
        if (size != 0) {
                System.out.println(this.toString());
        }

    }

//    public static void main(String[] args) {
//        ObjectList<String> list =new ObjectList<>();
//        for (int i = 0; i < 5; i++) {
//            list.add("data"+i);
//        }
//        for (int i = 0; i < list.size; i++) {
//            System.out.println(list.get(i));
//        }
//    }
}
